package cn.itsource.config;

import com.alibaba.cloud.commons.lang.StringUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * feign 的全局拦截
 */
@Slf4j
@Component
public class TokenInterceptor implements RequestInterceptor {


    private String tokenName = "satoken";

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String tokenValue = requestAttributes.getRequest().getHeader(tokenName);
        if(StringUtils.isNotEmpty(tokenValue)){
            requestTemplate.header(tokenName,tokenValue);
            log.warn("token转发 - {}" , tokenValue);
        }else{
            log.warn("token转发 - 请求头中并没有Token");
        }

    }
}